import axios from "axios";
import { Message } from 'element-ui'
// MessageBox
import { getToken } from "./auth";

// 创建axios实例
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 15000000, //请求超时间
})

// request 拦截器
service.interceptors.request.use(config => {
    let token = getToken()
    if (token) {
        // 让每个请求携带自定义token 请根据实际情况自行修改
        config.headers['token'] =  token;
    }
    return config
}, error => {
    console.log(error)
    Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(response => {
    /*
        根据code判断登录的状态等
    */
    return response.data;
}, error => {
    console.log('err' + error)
    Message({
        message: error.message,
        type: 'error',
        duration: 3 * 1000
    })
    return Promise.reject(error)
})

export default service